<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>菜单调试工具</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .btn {
            display: inline-block;
            padding: 10px 20px;
            margin: 5px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            border: none;
            cursor: pointer;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .info-box {
            background-color: #e7f3ff;
            padding: 15px;
            border-left: 4px solid #007bff;
            margin: 15px 0;
        }
        .error-box {
            background-color: #f8d7da;
            padding: 15px;
            border-left: 4px solid #dc3545;
            margin: 15px 0;
        }
        .success-box {
            background-color: #d4edda;
            padding: 15px;
            border-left: 4px solid #28a745;
            margin: 15px 0;
        }
        pre {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔧 菜单权限调试工具</h1>
        
        <div class="info-box">
            <h3>📋 当前登录状态</h3>
            <div id="loginStatus">检查中...</div>
        </div>

        <div class="info-box">
            <h3>🎯 用户信息</h3>
            <div id="userInfo">检查中...</div>
        </div>

        <div class="info-box">
            <h3>📋 菜单权限</h3>
            <div id="menuInfo">检查中...</div>
        </div>

        <div class="info-box">
            <h3>🔑 权限列表</h3>
            <div id="permissionInfo">检查中...</div>
        </div>

        <h3>🛠️ 调试操作</h3>
        <button class="btn" onclick="checkLoginStatus()">检查登录状态</button>
        <button class="btn" onclick="refreshMenus()">刷新菜单权限</button>
        <button class="btn" onclick="clearAllData()">清除所有数据</button>
        <button class="btn" onclick="forceLogin()">强制重新登录</button>

        <div id="result"></div>

        <h3>📝 解决方案</h3>
        <div class="info-box">
            <p><strong>如果菜单显示不完整，请尝试以下步骤：</strong></p>
            <ol>
                <li>点击"检查登录状态"查看详细信息</li>
                <li>确认用户角色是否为 system_admin</li>
                <li>点击"刷新菜单权限"重新获取菜单</li>
                <li>如果仍有问题，点击"强制重新登录"</li>
            </ol>
        </div>
    </div>

    <script>
        // 检查登录状态
        function checkLoginStatus() {
            const currentUser = localStorage.getItem('currentUser');
            const authToken = localStorage.getItem('authToken');
            const userMenus = localStorage.getItem('userMenus');
            const userPermissions = localStorage.getItem('userPermissions');

            let statusHtml = '';
            let userHtml = '';
            let menuHtml = '';
            let permissionHtml = '';

            if (currentUser && authToken) {
                statusHtml = '<div class="success-box">✅ 已登录</div>';
                
                try {
                    const user = JSON.parse(currentUser);
                    userHtml = `
                        <pre>${JSON.stringify(user, null, 2)}</pre>
                        <p><strong>用户角色:</strong> ${user.role}</p>
                        <p><strong>用户姓名:</strong> ${user.realName}</p>
                    `;

                    if (userMenus) {
                        const menus = JSON.parse(userMenus);
                        menuHtml = `
                            <p><strong>菜单数量:</strong> ${menus.length}</p>
                            <pre>${JSON.stringify(menus, null, 2)}</pre>
                        `;
                    } else {
                        menuHtml = '<div class="error-box">❌ 菜单数据为空</div>';
                    }

                    if (userPermissions) {
                        const permissions = JSON.parse(userPermissions);
                        permissionHtml = `
                            <p><strong>权限数量:</strong> ${permissions.length}</p>
                            <pre>${JSON.stringify(permissions, null, 2)}</pre>
                        `;
                    } else {
                        permissionHtml = '<div class="error-box">❌ 权限数据为空</div>';
                    }

                } catch (e) {
                    userHtml = '<div class="error-box">❌ 用户数据解析失败: ' + e.message + '</div>';
                }
            } else {
                statusHtml = '<div class="error-box">❌ 未登录</div>';
                userHtml = '<div class="error-box">❌ 无用户信息</div>';
                menuHtml = '<div class="error-box">❌ 无菜单数据</div>';
                permissionHtml = '<div class="error-box">❌ 无权限数据</div>';
            }

            document.getElementById('loginStatus').innerHTML = statusHtml;
            document.getElementById('userInfo').innerHTML = userHtml;
            document.getElementById('menuInfo').innerHTML = menuHtml;
            document.getElementById('permissionInfo').innerHTML = permissionHtml;
        }

        // 刷新菜单权限
        async function refreshMenus() {
            const currentUser = localStorage.getItem('currentUser');
            if (!currentUser) {
                showResult('请先登录', 'error');
                return;
            }

            try {
                const user = JSON.parse(currentUser);
                const authToken = localStorage.getItem('authToken');

                // 调用后端API重新获取菜单
                const response = await fetch('/api/users/menus', {
                    method: 'GET',
                    headers: {
                        'Authorization': 'Bearer ' + authToken,
                        'Content-Type': 'application/json'
                    }
                });

                if (response.ok) {
                    const result = await response.json();
                    if (result.result === 'SUCCESS') {
                        localStorage.setItem('userMenus', JSON.stringify(result.data.menuList || []));
                        localStorage.setItem('userPermissions', JSON.stringify(result.data.permissions || []));
                        showResult('菜单权限已刷新', 'success');
                        checkLoginStatus();
                    } else {
                        showResult('刷新失败: ' + result.message, 'error');
                    }
                } else {
                    showResult('请求失败: ' + response.status, 'error');
                }
            } catch (error) {
                showResult('刷新失败: ' + error.message, 'error');
            }
        }

        // 清除所有数据
        function clearAllData() {
            localStorage.clear();
            sessionStorage.clear();
            showResult('所有数据已清除', 'success');
            checkLoginStatus();
        }

        // 强制重新登录
        function forceLogin() {
            clearAllData();
            window.location.href = 'test-login.html';
        }

        // 显示结果
        function showResult(message, type) {
            const resultDiv = document.getElementById('result');
            const className = type === 'success' ? 'success-box' : 'error-box';
            resultDiv.innerHTML = `<div class="${className}">${message}</div>`;
        }

        // 页面加载时自动检查
        window.addEventListener('load', function() {
            checkLoginStatus();
        });
    </script>
</body>
</html>